package com.charles.oa.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.charles.oa.domain.Role;
import com.charles.oa.service.RoleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport implements ModelDriven<Role> {
	private static final long serialVersionUID = 1L;
	@Resource
	private RoleService roleService;
	private Long id;
	private String name;
	private String description;

	public String list() {
		List<Role> roles = roleService.findAll();
		ActionContext.getContext().put("roleList",roles);
		return "list";
	}

	public String add() {
		Role role = new Role();
		role.setName(name);
		role.setDescription(description);
		roleService.add(role);
		return "toList";
	}

	public String addUI() {
		return "addUI";
	}

	public String delete() {
		roleService.delete(id);
		return "toList";
	}

	public String edit() {
		Role role = roleService.getById(id);
		role.setDescription(description);
		role.setName(name);
		roleService.update(role);
		return "toList";
	}

	public String editUI() {
		Role role = roleService.getById(id);
		ActionContext.getContext().getValueStack().push(role);
		return "editUI";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	private Role role = new Role();

	@Override
	public Role getModel() {
		return role;
	}
}
